<?php

namespace common\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Article;

/**
 * ArticleSearch represents the model behind the search form about `common\models\Article`.
 */
class ArticleSearch extends Article
{
    /**
     * @inheritdoc
     */
    public $name; //<=====就是加在这里
    public function rules()
    {
        return [
            [['id', 'cate1', 'cate2', 'cate3', 'cate4', 'cate5', 'weight', 'status', 'created_at', 'updated_at'], 'integer'],
            [['article_id', 'article_name', 'content', 'res1', 'res2', 'res3','name'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Article::find()->joinWith(['cate']);

        //   $query->joinWith(['cate']);//<=====加入这句


        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $dataProvider->setSort([
            'attributes' => [
                /* 其它字段不要动 */
                /*  下面这段是加入的 */
                /*=============*/
                'name' => [
                    'asc' => ['c_article_cate.name' => SORT_ASC],
                    'desc' => ['c_article_cate.name' => SORT_DESC],
                    'label' => 'cate Name'
                ],
                /*=============*/
            ]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'c_article.id' => $this->id,
            'cate1' => $this->cate1,
            'cate2' => $this->cate2,
            'cate3' => $this->cate3,
            'cate4' => $this->cate4,
            'cate5' => $this->cate5,
            'weight' => $this->weight,
            'status' => $this->status,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]);

        $query->andFilterWhere(['like', 'article_id', $this->article_id])
            ->andFilterWhere(['like', 'article_name', $this->article_name])
            ->andFilterWhere(['like', 'content', $this->content])
            ->andFilterWhere(['like', 'c_article_cate.name', $this->name])//<=====加入这句
            ->andFilterWhere(['like', 'res1', $this->res1])
            ->andFilterWhere(['like', 'res2', $this->res2])
            ->andFilterWhere(['like', 'res3', $this->res3]);
        return $dataProvider;
    }
}



